import {
    Canvas,
    CanvasRenderingContext2D,
    createCanvas,
    NodeCanvasRenderingContext2DSettings,
    registerFont
} from 'canvas';

import fs from "fs";
import path from "node:path";

import { fileURLToPath } from 'url';
import { dirname } from 'path';

// @ts-ignore
const __filename = fileURLToPath(import.meta.url); // 当前文件绝对路径
const __dirname = dirname(__filename);

export class NodeCanvas {
    canvas: Canvas;
    ctx: CanvasRenderingContext2D;

    constructor(width, height) {
        const canvas = createCanvas(width, height);
        this.ctx = canvas.getContext('2d')
        this.canvas = canvas

        this.ctx.fillStyle = '#fff';
        this.ctx.fillRect(0, 0, width, height);
        this.ctx.fillStyle = '#000';
        this.loadFont()
    }

    drawText(
        text: string,
        x: number,
        y: number,
        style?: {
            fillStyle?: CanvasRenderingContext2D['fillStyle'],
            font?: CanvasRenderingContext2D['font'],
            textAlign?: CanvasRenderingContext2D['textAlign'],
            textBaseline?: CanvasRenderingContext2D['textBaseline']
        }) {
        if (style) {
            Object.keys(style).forEach((key) => {
                this.ctx[key] = style[key]
            })
        }
        this.ctx.fillText(text, x, y);
        return this;
    }

    /**
     * 画线
     * @param x
     * @param y
     * @param x2
     * @param y2
     * @param lineWidth
     */
    stroke(x: number, y: number, x2: number, y2: number, lineWidth: number = 1) {
        const ctx = this.ctx
        // 设置线条样式
        ctx.strokeStyle = '#000';
        ctx.lineWidth = lineWidth;

// 设置虚线样式 [实线长度, 空白长度]
        ctx.setLineDash([5, 3]);

// 绘制一条虚线
        ctx.beginPath();
        this.ctx.moveTo(x+0.5, y+0.5);
        this.ctx.lineTo(x2+0.5, y2+0.5);
        ctx.stroke();
        return this
    }

    /**
     * 画虚线
     * @param x
     * @param y
     * @param x2
     * @param y2
     * @param lineWidth
     */
    strokeDash(x: number, y: number, x2: number, y2: number, lineWidth: number = 1) {
        this.ctx.setLineDash([5, 3]);
        return this.stroke(x, y, x2, y2, lineWidth)

    }

    /**
     * 画矩形
     * @param x
     * @param y
     * @param x2
     * @param y2
     * @param lineWidth
     */
    strokeRect(x: number, y: number, x2: number, y2: number, lineWidth: number = 1) {
        this.ctx.lineWidth = lineWidth;
        this.ctx.strokeRect(x, y, x2, y2);
        return this;
    }

    /**
     * 画虚线的矩形
     * @param x
     * @param y
     * @param x2
     * @param y2
     * @param lineWidth
     */
    strokeDashRect(x: number, y: number, x2: number, y2: number, lineWidth: number = 1) {
        // 设置虚线样式 [实线长度, 空白长度]
        this.ctx.setLineDash([5, 3]);
        this.ctx.lineWidth = lineWidth;
        this.ctx.strokeRect(x + 0.5, y + 0.5, x2 - 1, y2 - 1);
        return this;
    }

    loadFont() {
        registerFont(path.join(__dirname,'./font/ddjinbu.ttf'), {family: 'ddjinbu'});
        registerFont(path.join(__dirname,'./font/miaozi.ttf'), {family: 'miaozi'});
    }

    toBase64() {
        const buffer = this.canvas.toBuffer('image/png');
        fs.writeFileSync(path.join(__dirname,'./image/node-canvas.png'), buffer);
        return this.canvas.toDataURL();
    }
}



